package com.zhoug.permission2;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

/**
 * MD5加密算法
 *
 * @author 1
 */
class MUtils {
    private static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f'};

    public static String getMD5(String s) {
        byte[] strTemp = s.getBytes();
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(strTemp);
            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);

        } catch (Exception e) {
            return s;
        }
    }

    public static String toString(String[] arrays){
        String result=null;
        if(arrays!=null){
            StringBuilder builder=new StringBuilder();
            builder.append("[");
            int length = arrays.length - 1;
            for (int i = 0; i <=length ; i++) {
                builder.append(arrays[i]);
                if(i<length){
                    builder.append(",");
                }
            }
            builder.append("]");
            result=builder.toString();
        }
        return result;
    }

    public static String getNames(List<String> permissions){
        if(permissions==null|| permissions.size()==0){
            return null;
        }
        List<String> names=new ArrayList<>();
        for (int i = 0; i < permissions.size(); i++) {
            String groupName = Permission.getGroupName(permissions.get(i));
            if(!names.contains(groupName)){
                names.add(groupName);
            }
        }
        StringBuilder builder=new StringBuilder();
        int length = names.size() - 1;
        for (int i = 0; i <=length; i++) {
            builder.append(names.get(i));
            if(i<length){
                builder.append(",");
            }
        }
        return builder.toString();
    }

}
